# @Time: 2023/1/26 19:46
# @Author: 张江南
# @File: 
# @Project: 代码
import pymysql
import xlwings as xw

con = pymysql.connect(host='localhost', user='root', password='123qweQAZ', database='ry-vue', charset='utf8')
cur = con.cursor()

try:
    sql2 = 'insert into article_info (origin,title,pictures,type,author_name,author_id,description,create_date,' \
           'watch_num,collect_num,remark,content_id) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)'
    param = ("123", "123", "123", "123", "123", "123", "123", "2022-12-20 12:30:09", "123", "123", "123",
             "890")
    cur.execute(sql2, param)
    # 最后插⼊⾏的主键id
    print(cur.lastrowid)
    # 最新插⼊⾏的主键id
    print(con.insert_id())
    # 提交事务 方法二
    con.commit()
except Exception as e:
    # 如果执行失败要回滚
    con.rollback()
    print("数据库操作异常：\n", e)
finally:
    cur.close()
    # 关闭数据库连接，目的为了释放内存
    con.close()
